import { ajax } from '../../ajax';

Page({
  data: {
    status: 1, //1是付款成功
    second: 3,
    buyid:'',
    status:'',
    total_amount:'',
    PayWays:'',
    pay_status_desc:'',
    out_trade_no:'',
    countdown:'',
    print: null,
    roomNum:''
  },

toHome() {
  clearInterval(this.data.countdown);
  my.reLaunch({url:'../home/home'})
},

// 键盘监听
onKeyPress(r) {
    switch (r.keyCode) {
      case 131:
        r.keyName = '收款';
        console.log(r.amount)
        break;
      case 132:
        r.keyName = '刷脸';
        console.log('刷脸')
        break;
      case 133:
        r.keyName = '取消';
        console.log('取消')
        break;
      case 134:
        r.keyName = '设置';
        console.log('设置')
        break;
    }
    console.log('KeyEvent', r);
},


getData() {
  let token = my.getStorageSync({ key: 'token' }).data;
  ajax('/SmallAppSmilePayQuery',{ token, out_trade_no:this.data.out_trade_no }).then((res) => {
    console.log(res)
    this.setData({
      status: res.data.data.pay_status,
      total_amount: res.data.data.total_amount,
      buyid: res.data.data.buyer_id,
      PayWays: res.data.data.PayWays,
      pay_status_desc: res.data.data.pay_status_desc,
      print: res.data.data.print
    })
  })
    // if(res.data.data.status === 1) {
      // this.status(res.data.data.print)
    // }
},
getFundData(){
  let token = my.getStorageSync({ key: 'token' }).data;
  // fu2019120416515524359197
  ajax('/FundAuthOrderDetailQuery',{ token, out_order_no: this.data.out_trade_no }).then((res) => {
    console.log(res)
    let resData = res.data.data[0];
    console.log(resData)
    this.setData({
      status: resData.pay_status,
      total_amount: resData.amount,
      buyid: resData.payer_logon_id,
      PayWays: resData.PayWays,
      pay_status_desc: resData.pay_status_desc,
      print: resData.print
    })
  })
},
// 打印内容
printer(data) {
  let { PayWay, cashier_name, out_trade_no, pay_status, print_time, qr_code, remark, store_name, total_amount, trade_no, updated_at } = data;
  //查询打印机连接状态
  my.ix.printerStatus({
    success: (r) => {
      my.ix.printer({
        cmds: [
              {'cmd':'addSelectPrintModes', 'args':['FONTA', 'OFF', 'OFF', 'OFF', 'ON']}, 
              {'cmd':'addSelectJustification', 'args': ['CENTER']},
              {'cmd':'addText', 'args':[store_name]},
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addText', 'args':['']},
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addSelectPrintModes', 'args':['FONTA', 'OFF', 'OFF', 'OFF', 'OFF']},
              {'cmd':'addSelectJustification', 'args': ['CENTER']},
              {'cmd':'addTurnDoubleStrikeOnOrOff', 'args': ['ON']},
              {'cmd':'addText', 'args':[store_name]},
              {'cmd':'addPrintAndLineFeed', 'args':[]}, //9
              
              {'cmd':'addPrintAndFeedLines', 'args':['1']},
              {'cmd':'addSelectJustification', 'args': ['LEFT']},
              {'cmd':'addTurnDoubleStrikeOnOrOff', 'args': ['OFF']},
              {'cmd':'addText', 'args':['支付方式：'+ PayWay === 'alipay'? '支付宝收款' : '微信收款']},
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addSelectJustification', 'args': ['LEFT']},
              {'cmd':'addText', 'args':['订单类型：'+ pay_status === 1? '收款' : '退款']}, //15
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addSelectJustification', 'args': ['LEFT']},
              {'cmd':'addText', 'args':['交易时间：'+ updated_at ]},
              {'cmd':'addPrintAndLineFeed', 'args':[]},
              {'cmd':'addSelectJustification', 'args': ['LEFT']}, //20
              
              {'cmd':'addText', 'args':['===============================']},
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addSelectJustification', 'args': ['LEFT']},
              {'cmd':'addText', 'args':['订单号：'+ out_trade_no]},
              {'cmd':'addPrintAndLineFeed', 'args':[]},
              {'cmd':'addSelectJustification', 'args': ['LEFT']},
              {'cmd':'addText', 'args':['流水号：'+ trade_no]},
              {'cmd':'addPrintAndLineFeed', 'args':[]},
              {'cmd':'addSelectJustification', 'args': ['LEFT']},
              {'cmd':'addText', 'args':['===============================']}, //30
              {'cmd':'addPrintAndLineFeed', 'args':[]},
              {'cmd':'addSelectJustification', 'args': ['LEFT']},
              {'cmd':'addText', 'args':['收款：']},
              {'cmd':'addText', 'args':['￥'+ total_amount]},
              {'cmd':'addPrintAndLineFeed', 'args':[]},
              {'cmd':'addSelectJustification', 'args': ['LEFT']},
              {'cmd':'addText', 'args':['备注：'+ remark]},
              {'cmd':'addPrintAndLineFeed', 'args':[]},
              {'cmd':'addSelectJustification', 'args': ['LEFT']},
              {'cmd':'addText', 'args':['===============================']}, //40
              {'cmd':'addPrintAndLineFeed', 'args':[]},
              // 二维码
              {'cmd':'addSelectJustification', 'args': ['CENTER']}, /*设置打印居中对齐*/
              {'cmd':'addStoreQRCodeDataGB18030', 'args':['https://www.baidu.com']}, /*设置qrcode内容*/
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addSelectJustification', 'args': ['LEFT']},
              {'cmd':'addText', 'args':['===============================']}, 
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addText', 'args':['收银员：'+ cashier_name]}, 
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addText', 'args':['打印时间：'+ print_time ]}, 
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addText', 'args':['']},
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addSelectJustification', 'args': ['CENTER']},
              {'cmd':'addText', 'args':['天择可付 智慧收银']}, 
              {'cmd':'addPrintAndLineFeed', 'args':[]},


              {'cmd':'addText', 'args':['']},
              {'cmd':'addPrintAndLineFeed', 'args':[]},

              {'cmd':'addSelectPrintModes', 'args':['FONTA', 'OFF', 'ON', 'ON', 'OFF']},
              {'cmd':'addText', 'args':['小票编号：0503-1']}, 
              {'cmd':'addPrintAndLineFeed', 'args':[]},
              {'cmd':'addPrintAndFeedLines', 'args':['1']},

            
              ],
        success: (r) => {
          console.log("success");
        },
        fail: (r) => {
          console.log("fail, errorCode:" + r.error);
        }
      });
    },
    fail: (r) => {
      my.showToast({content: '打印机未连接'})
    }
  });

},


onLoad(option) {
  console.log(option)
  this.setData({out_trade_no:option.out_trade_no})
  // 普通订单详情
  if(option.result_status == 1) this.getData();
  // 预授权订单详情
  else this.getFundData();
  console.log(this.data.out_trade_no)
},

onShow() {
  // this.getData();
  // this.getFundData();
  this.data.countdown = setInterval(()=>{
    let s = this.data.second - 1 ;
    // console.log(s)
    this.setData({second: s})
    if(this.data.second < 1) {
      clearInterval(this.data.countdown);
      console.log('暂停', this.data.second)
      my.reLaunch({url:'../home/home'});
  }
  },1000)

},


  onHide() {
    clearInterval(this.data.countdown);
  },
  
  onUnload() {
     clearInterval(this.data.countdown);
  }

});


